package com.qfhd.soft.cjzcore.Core;

import android.util.Base64;

import com.qfhd.soft.cjzcore.CJZConstant;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;

// 加密方式 先异或  然后base64
public class CJCrypto {

	// 获取解密数据
	public static String GetDecryptoString_customKey(String str,String key) {
		return new String(xorEncrypt(base64decodeToBytes(str), key.getBytes()));
	}

	// 获取加密数据
	public static String GetEncryptString_customKey(String str,String key) {
		return base64encode(xorEncrypt(str.getBytes(), key.getBytes()));
	}

	// 获取解密数据
	public static String GetDecryptoString(String str) {
		return new String(xorEncrypt(base64decodeToBytes(str), CJZConstant.ACryptoKey.getBytes()));
	}

	// 获取加密数据
	public static String GetEncryptString(String str) {
		return base64encode(xorEncrypt(str.getBytes(), CJZConstant.ACryptoKey.getBytes()));
	}

	// 获取解密数据
	public static String GetDecryptoString(String str, String key) {
		return new String(xorEncrypt(base64decodeToBytes(str), key.getBytes()));
	}

	// 获取加密数据
	public static String GetEncryptString(String str, String key) {
		return base64encode(xorEncrypt(str.getBytes(), key.getBytes()));
	}

	public static final InputStream byte2Input(byte[] buf) {
		return new ByteArrayInputStream(buf);
	}

	public static final byte[] input2byte(InputStream inStream) throws IOException {
		ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
		final int buffSize = 64;
		byte[] buff = new byte[buffSize];
		int rc = 0;
		while ((rc = inStream.read(buff, 0, buffSize)) > 0) {
			swapStream.write(buff, 0, rc);
		}
		byte[] in2b = swapStream.toByteArray();
		return in2b;
	}

	public static void xorEncryptFile(String sourcePath, String destPath, String key) {
		try {
			File sourceFile = new File(sourcePath);
			File destFile = new File(destPath);
			byte[] keyBytes = key.getBytes();
			int keyLength = keyBytes.length;
			FileInputStream fs_source = new FileInputStream(sourceFile);
			FileOutputStream fs_dest = new FileOutputStream(destFile);
			int read;
			int pos = 0;
			while ((read = fs_source.read()) > -1) {
				fs_dest.write(read ^ keyBytes[pos % keyLength]);
			}
			fs_dest.flush();
			fs_dest.close();
			fs_source.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String xorEncryptString(String source, String key) {
		return new String(xorEncrypt(source.getBytes(), key.getBytes()));
	}

	public static byte[] xorEncrypt(byte[] source, byte[] key) {
		byte[] result = source;
		do {
			if (source == null || source.length == 0)
				break;
			int pos = 0;
			do {
				result[pos] = (byte) (source[pos] ^ key[pos % key.length]);
				pos++;
				if (pos >= source.length)
					break;
			} while (true);
		} while (false);
		return source;
	}

	public static String base64encode(String str) {
		return new String(Base64.encode(str.getBytes(), Base64.NO_WRAP));
	}

	public static String base64encode(byte[] str) {
		return new String(Base64.encode(str, Base64.NO_WRAP));
	}

	public static String base64decodeToString(String str) {
		return new String(Base64.decode(str.getBytes(), Base64.DEFAULT));
	}

	public static byte[] base64decodeToBytes(String str) {
		return Base64.decode(str.getBytes(), Base64.DEFAULT);
	}

	// 计算文件MD5
	public static String GetFileMd5(String packagePath) {
		try {
			MessageDigest sig = MessageDigest.getInstance("MD5");
			File packageFile = new File(packagePath);
			InputStream signedData = new FileInputStream(packageFile);
			byte[] buffer = new byte[4096];
			long toRead = packageFile.length();
			long soFar = 0;
			boolean interrupted = false;
			while (soFar < toRead) {
				interrupted = Thread.interrupted();
				if (interrupted)
					break;
				int read = signedData.read(buffer);
				soFar += read;
				sig.update(buffer, 0, read);
			}
			signedData.close();
			byte[] digest = sig.digest();
			String digestStr = BytesToHexString(digest);
			return digestStr;

		} catch (Exception e) {

		}
		return "";
	}

	// 计算字符串的MD5
	public static String GetStringMd5(String str) {
		try {
			MessageDigest sig = MessageDigest.getInstance("MD5");
			sig.update(str.getBytes(), 0, str.getBytes().length);
			byte[] digest = sig.digest();
			String digestStr = BytesToHexString(digest);
			return digestStr;

		} catch (Exception e) {

		}
		return "";
	}

	// bytes to hex string
	public static String BytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		int i = 0;
		while (i < src.length) {
			int v;
			String hv;
			v = (src[i] >> 4) & 0x0F;
			hv = Integer.toHexString(v);
			stringBuilder.append(hv);
			v = src[i] & 0x0F;
			hv = Integer.toHexString(v);
			stringBuilder.append(hv);
			i++;
		}
		return stringBuilder.toString();
	}
	
	public static String bytesToHex(byte[] data) {
		int len = data.length;
		String str = "";
		for (int i = 0; i < len; i++) {
			if ((data[i] & 0xFF) < 16)
				str = str + "0" + Integer.toHexString(data[i] & 0xFF);
			else
				str = str + Integer.toHexString(data[i] & 0xFF);
		}
		return str;
	}

	public static byte[] hexToBytes(String str) {
		int len = str.length() / 2;
		byte[] buffer = new byte[len];
		for (int i = 0; i < len; i++) {
			buffer[i] = (byte) Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16);
		}
		return buffer;
	}
}
